package com.qf.service.impl;

import com.qf.entity.*;
import com.qf.entity.custom.ProductCustom;
import com.qf.entity.custom.ShopCustom;
import com.qf.entity.custom.UserSearchCustom;
import com.qf.mapper.UserSearchMapper;
import com.qf.mapper.UserShareMapper;
import com.qf.mapper.custom.UserSearchCustomMapper;
import com.qf.service.UserSearchService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ZhangYubin
 * @date: 2020/7/24 21:57
 * @description:
 */
@Service
public class UserSearchServiceImpl implements UserSearchService {
    @Autowired
    private UserSearchMapper userSearchMapper;
    @Autowired
    private UserSearchCustomMapper userSearchCustomMapper;

    /**
     * 搜索后增加点击量
     * @param userSearchCustom 用户搜索商品和店铺id
     * @param userId 用户id
     * @return 成功返回true 失败返回false
     */
    @Override
    public boolean insertUserSearchByCount(HashMap userSearchCustom, Integer userId) {

        Integer productId = (Integer) userSearchCustom.get("productId");
        Integer shopId = (Integer) userSearchCustom.get("shopId");
        if (userId != null) {
            List<UserSearch> userSearches = userSearchCustomMapper.selectUserSearchById(userId);
            if (userSearches != null) {
                if (productId != null) {
                    List<UserSearch> userSearches1 = userSearchCustomMapper.selectUserSearchById(productId);
                    if (userSearches1 != null) {
                        for (UserSearch u : userSearches1) {
                            Integer count = u.getCount();
                            u.setCount(++count);
                            int i = userSearchMapper.updateByExampleSelective(u, null);
                            if (i != 0) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                        return true;
                    } else {
                        UserSearch userSearch = new UserSearch();
                        userSearch.setUserId(userId);
                        userSearch.setProductId(productId);
                        userSearch.setCount(1);
                        int i = userSearchMapper.insert(userSearch);
                        if (i != 0) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }
                if (shopId != null) {
                    List<UserSearch> userSearches2 = userSearchCustomMapper.selectUserSearchById(shopId);
                    if (userSearches2 != null) {
                        for (UserSearch u : userSearches2) {
                            Integer count = u.getCount();
                            u.setCount(++count);
                            int i = userSearchMapper.updateByExampleSelective(u, null);
                            if (i != 0) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                        return true;
                    } else {
                        UserSearch userSearch = new UserSearch();
                        userSearch.setUserId(userId);
                        userSearch.setShopId(shopId);
                        userSearch.setCount(1);
                        int i = userSearchMapper.insertSelective(userSearch);
                        if (i != 0) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                } else {
                    return false;
                }
            } else {
                UserSearch userSearch = new UserSearch();
                userSearch.setUserId(userId);
                if (productId != null) {
                    userSearch.setProductId(productId);
                }
                if (shopId != null) {
                    userSearch.setProductId(shopId);
                }
                int i = userSearchMapper.insertSelective(userSearch);
                if (i != 0) {
                    return true;
                } else {
                    return false;
                }
            }
        }else {
            return false;
        }
    }

    /**
     * 热门商品名
     * @param id 商品id
     * @return 返回UserSearchCustom的list集合
     */
    @Override
    public List<UserSearchCustom> selectAllProductByCount(int id) {
        return userSearchCustomMapper.selectAllProductByCount(id);
    }

    /**
     * 热门店铺名
     * @param id 店铺id
     * @return 返回UserSearchCustom的list集合
     */
    @Override
    public List<UserSearchCustom> selectAllShopByCount(int id) {
        return userSearchCustomMapper.selectAllShopByCount(id);
    }
}
